package cn.sise.edu.crm.service.impl;

import cn.sise.edu.crm.domain.Customer;
import cn.sise.edu.crm.domain.Employee;
import cn.sise.edu.crm.mapper.CustomerMapper;
import cn.sise.edu.crm.query.QueryObject;
import cn.sise.edu.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void insert(Customer customer) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        customer.setInputUser(employee);
        customer.setInputTime(new Date());
        customerMapper.insert(customer);
    }


    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public Customer selectById(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }

    @Override
    public PageInfo<Customer> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(customerMapper.selectForList(qo));
    }

    @Override
    public void updateByStatus(Customer customer) {
        customerMapper.updateByStatus(customer);
    }
}
